// xredis
package main

import (
	"flag"
	"fmt"
	"github.com/gosexy/redis"
	"strings"
	"time"
)

type Server struct {
	Version          string
	Git_Sha1         string
	Git_Dirty        string
	Build_Id         string
	Mode             string
	Os               string
	Arch_Bit         string
	Multiplexing_Api string
	Process_Id       string
	Run_Id           string
	Tcp_Port         string
	Up_Seconds       string
	Up_Days          string
	Hz               string
	Lru_Clock        string
	Cf_File          string
}

type Clients struct {
	Conn_Clients    int64
	Clients_list    int64
	Clients_Buf     int64
	Blocked_clients int64
}

type Memory struct {
	Used_Mem                int64
	Used_Mem_Human          float64
	Used_Mem_Rss            int64
	Used_Mem_Peak           int64
	Used_Mem_Peak_Human     float64
	Used_Mem_Lua            int64
	Mem_Fragmentation_Ratio float64
	Mem_Allocator           string
}

type Persistence struct {
	Loading                      int64
	Rdb_changes_since_last_save  int64
	Rdb_bgsave_in_progress       int64
	Rdb_last_save_time           int64
	Rdb_last_bgsave_status       string
	Rdb_last_bgsave_time_sec     int64
	Rdb_current_bgsave_time_sec  int64
	Aof_enabled                  int
	Aof_rewrite_in_progress      int
	Aof_rewrite_scheduled        int
	Aof_last_rewrite_time_sec    int
	Aof_current_rewrite_time_sec int
	Aof_last_bgrewrite_status    string
	Aof_last_write_status        string
}

type Stats struct {
	Total_conn_received       int
	Total_cmds_processed      int
	Instantaneous_ops_per_sec int
	Total_net_input_bytes     int64
	Total_net_output_bytes    int64
	Instantaneous_input_kbps  float64
	Instantaneous_output_kbps float64
	Rejected_conn             int
	Sync_full                 int
	Sync_partial_ok           int
	Sync_partial_err          int
	Expired_keys              int
	Eviceted_keys             int64
	Keyspace_hits             int
	Keyspace_misses           int
	Pubsub_channels           int
	Pubsub_patterns           int
	Lastest_fork_usec         int
}

type Replication struct {
	Role                           string
	Conn_slaves                    int
	Master_repl_offset             int
	Repl_backlog_active            int
	Repl_backlog_size              int64
	Repl_backlog_first_byte_offset int
	Repl_backlog_histlen           int
}

type Cpu struct {
	Used_cpu_sys           float64
	Used_cpu_user          float64
	Used_cpu_sys_children  float64
	Used_cpu_user_children float64
}

func GetInfo(c *redis.Client, sec string) map[string]string {
	s, err := c.Info(sec)
	if err != nil {
		fmt.Println("get key failure")
		return nil
	}
	a1 := strings.Split(s, "\n")
	m1 := make(map[string]string)
	for i := 1; i < len(a1); i++ {
		if !strings.Contains(a1[i], ":") {
			break
		}
		a2 := strings.Split(a1[i], ":")
		m1[a2[0]] = a2[1]
	}
	return m1
}

func main() {
	//fmt.Println("Hello World!")
	T_out := time.Second * 10
	ip := flag.String("h", "127.0.0.1", "redis server ip")
	port := flag.Uint("p", 6379, "redis server port")
	//pass := flag.String("w", "", "redis server password")
	timeout := flag.Duration("t", T_out, "connect redis timeout")
	flag.Parse()
	client := redis.New()
	err := client.ConnectWithTimeout(*ip, *port, *timeout)
	if err != nil {
		fmt.Println("connect redis-server failure")
		return
	}
	m1 := GetInfo(client, "Server")
	fmt.Println(m1["redis_version"])
	client.Quit()

}
